package com.xinchuang.controller.targetSystem.weapon;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xinchuang.common.Result;
import com.xinchuang.common.ResultUtil;
import com.xinchuang.entity.targetSystem.object.SpecialAdd;
import com.xinchuang.entity.targetSystem.weapon.WeaponMessage;
import com.xinchuang.entity.targetSystem.weapon.WeaponRelation;
import com.xinchuang.entity.targetSystem.weapon.WeaponType;
import com.xinchuang.service.targetSystem.weapon.WeaponHouseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @ClassName WeaponHouseController
 * @Description: TODO(写一段描述的话)
 * @Author zhangxuan
 * @Date 2022-08-17 15:03
 * @Verion V1.0
 */
@Api(value = "武器库接口", tags = {"武器库维护接口"})
@RequestMapping("/weaponHouse")
@RestController
public class WeaponHouseController {

    private final String TREE = "/tree";

    private final String TREE_DATA = "/treeData";

    private final String TREE_SORT = "/treeSort";

    private final String TREE_PULL = "/treePull";

    private final String WEAPON_MESSAGE = "/weaponMessage";

    private final String WEAPON_PULL = "/weaponPull";

    private final String WEAPON_DATA = "/weaponData";

    private final String WEAPON_MESSAGE_DATA = "/weaponMessageData";

    @Autowired
    private WeaponHouseService weaponHouseService;

    @ApiOperation(value = "查询类别树")
    @GetMapping(TREE)
    public Result selectTree() {
        return weaponHouseService.selectType();
    }

    @ApiOperation(value = "新增/修改类别树")
    @PostMapping(TREE)
    public Result addTree(
            @RequestBody WeaponType weaponType
    ) {
        return weaponHouseService.addOrUpdateType(weaponType);
    }

    @ApiOperation(value = "删除类别树")
    @DeleteMapping(TREE)
    public Result deleteTree(
            @RequestParam(value = "typeId", required = false) String typeId
    ) {
        return weaponHouseService.delType(typeId);
    }

    @ApiOperation(value = "查询武器库属性")
    @GetMapping(TREE_DATA)
    public Result<Map<String, Object>> selectWeaponData(
            @RequestParam(value = "pageNum", required = false) Integer pageNum,
            @RequestParam(value = "pageSize", required = false) Integer pageSize,
            @RequestParam(value = "typeId", required = false) String typeId
    ) {
        return weaponHouseService.selectWeaponAttribute(pageNum, pageSize, typeId);
    }

    @ApiOperation(value = "新增/修改武器库属性")
    @PostMapping(TREE_DATA)
    public Result addSpecial(
            @Validated @RequestBody SpecialAdd specialAdd
    ) {
        return weaponHouseService.addOrUpdateWeaponAttribute(specialAdd);
    }

    @ApiOperation(value = "删除武器库属性")
    @DeleteMapping(TREE_DATA)
    public Result deleteSpecial(
            @RequestParam(value = "id") String id
    ) {
        return weaponHouseService.delWeaponAttribute(id);
    }

    @ApiOperation(value = "武器库属性排序")
    @GetMapping(value = TREE_SORT)
    public Result getSpecialSort(
            @RequestParam(value = "id") String id,
            @RequestParam(value = "sign") int sign
    ) {
        return weaponHouseService.getWeaponSort(id, sign);
    }

    @ApiOperation(value = "下拉列表选择类别")
    @GetMapping(TREE_PULL)
    public Result selectTypePullDowns() {
        return weaponHouseService.selectTypePull();
    }

    @ApiOperation(value = "查询武器库数据信息")
    @GetMapping(WEAPON_MESSAGE)
    public Result selectWeaponMessageAll(
            @RequestParam(value = "id") String id
    ) {
        return weaponHouseService.selectWeaponMessage(id);
    }

    @ApiOperation(value = "新增/修改你武器库数据信息")
    @PostMapping(WEAPON_MESSAGE)
    public Result addWeaponMessageAll(
            @RequestBody JSONObject json
            ) {
        String id = json.getString("id");
        JSONArray list = json.getJSONArray("dataList");
        if (StringUtils.isNotBlank(id) && CollectionUtil.isNotEmpty(list)) {
            List<WeaponMessage> messages = list.toJavaList(WeaponMessage.class);
            return weaponHouseService.addOrUpdateWeaponMessage(id, messages);
        }
        return ResultUtil.error(400, "数据异常请重试");
    }

    @ApiOperation(value = "查询武器库数据下拉")
    @GetMapping(WEAPON_PULL)
    public Result selectWeaponPullAll(
            @RequestParam(value = "typeId") String id
    ) {
        return weaponHouseService.selectWeaponMessagePull(id);
    }

    @ApiOperation(value = "新增/修改目标武器库关联信息")
    @PostMapping(WEAPON_DATA)
    public Result addWeaponData(
            @RequestBody JSONObject json
    ) {
        if (ObjectUtil.isNull(json)) {
            return ResultUtil.error(400, "请检查数据后重试");
        }
        WeaponRelation weaponRelation = new WeaponRelation();
        weaponRelation.setId(json.getString("id"));
        weaponRelation.setWeaponId(json.getString("weaponId"));
        weaponRelation.setTargetId(json.getString("targetId"));
        weaponRelation.setTypeId(json.getString("typeId"));
        weaponRelation.setPosition(json.getString("position"));
        weaponRelation.setRemark(json.getString("angle"));
        return weaponHouseService.addWeaponData(weaponRelation);
    }

    @ApiOperation(value = "删除目标武器库关联信息")
    @DeleteMapping(WEAPON_DATA)
    public Result delWeaponData(
            @RequestParam(value = "id") String id
    ) {
        return weaponHouseService.delWeaponData(id);
    }

    @ApiOperation(value = "查询武器库信息")
    @GetMapping(WEAPON_MESSAGE_DATA)
    public Result selectWeaponMessageData(
            @RequestParam(value = "id") String id
    ) {
        return weaponHouseService.selectWeaponMessageByWeaponId(id);
    }

    @ApiOperation(value = "查询目标武器库关联信息")
    @GetMapping(WEAPON_DATA)
    public Result selectWeaponData(
            @RequestParam(value = "id") String id
    ) {
        return weaponHouseService.selectWeaponData(id);
    }

}